// pages/recharge/recharge.js
import {
	userCenter,
	recharge
} from '../../utils/request/apiurl'
import api from '../../utils/request/api'

Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		// numList: [{
		// 		action: true,
		// 		value: 10
		// 	},
		// 	{
		// 		action: false,
		// 		value: 50
		// 	},
		// 	{
		// 		action: false,
		// 		value: 100
		// 	},
		// 	{
		// 		action: false,
		// 		value: 150
		// 	}
		// ],
		numList:[],//充值列表
		uid: wx.getStorageSync('userInfo').uid,
		userData: '', //个人信息
		addPrice: '', //充值金额
		givePrice:'',//赠送金额
		// nav:0,//索引值
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function(options) {
		this.getInfo()
    this.getAddPriceList()
	},
	// 获取个人信息
	getInfo() {
		api.post(userCenter.userData, {
			uid: wx.getStorageSync('userInfo').uid
		}).then(res => {
			// console.log(res)
			this.setData({
				userData: res
			})
		}).catch(err => {
			wx.showToast({
				title: err.message,
				icon: 'none'
			})
		})
	},


	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function() {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function() {
    this.getAddPriceList()
	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function() {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function() {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function() {
		wx.showNavigationBarLoading() //在标题栏中显示加载
		this.setData({
			numList:[]
		})
		this.onLoad()
		setTimeout(function() {
			wx.hideNavigationBarLoading() //完成停止加载
			wx.stopPullDownRefresh() //停止下拉刷新
		}, 1500);
	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function() {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function() {

	},
	// 充值列表 
	getAddPriceList(){
		api.post(recharge.list).then(res => {
			// console.log(res,'充值')
			var numList=[]
			
			for(var i=0;i<res.length;i++){
				// debugger
				var obj={}
				obj['action']=false
				obj['value']=res[i].addPrice
				obj['givePrice']=res[i].givePrice	
				numList.push(obj)
			}			
			// console.log(numList)
			this.setData({
        numList: numList,
        //默认第一个值
        addPrice: numList[0].value
      })
			if(this.data.numList.length>0){
				this.setData({
					'numList[0].action': true,
				})
			}
			
		}).catch(err => {
			// console.log(err)
			wx.showToast({
				title: err.message,
				icon: 'none'
			})
		})	
	},
	// 切换金额
	changeTab(e) {
		// console.log(e)
		// debugger
		let Index = e.currentTarget.dataset['index']
		this.data.numList.map((a, nowIndex) => {
			// debugger
			a.action = false
			// console.log(a)
			if (nowIndex === Index) {
				// console.log(a.action)
				a.action = true
			}
			return a
			// this.setData({
			// 	numList: this.data.numList,
			// 	addPrice: e.currentTarget.dataset.value,
			// 	givePrice: e.currentTarget.dataset.giveprice
			// })
		})
		this.setData({
			numList: this.data.numList,
			addPrice: e.currentTarget.dataset.value,
			givePrice: e.currentTarget.dataset.giveprice
		})
	},
	// 充值记录
	toList() {
		wx.navigateTo({
			url: '../cashWithdrawal-list/cashWithdrawal-list?type=1'
		})
	},
	// 充值
	recharge(e) {
		// console.log(this.data.addPrice)
		// console.log(this.data.numList[0].value)
    console.log(e, "=========>", this.data.numList)
    if (this.data.addPrice == '' || this.data.addPrice == 0){
      return wx.showToast({
								title: '不能充值0元',
								icon: 'none'
							})
			var source = {
				openid: wx.getStorageSync('userInfo').xcxOpenid,
				addPrice: this.data.numList[0].value,
				givePrice:this.data.numList[0].givePrice,
        formId: e.detail.formId
			}
		}else{
			var source = {
        openid: wx.getStorageSync('userInfo').xcxOpenid,
				addPrice:this.data.addPrice,
        givePrice: this.data.givePrice,
        formId: e.detail.formId
			}
		}
		// console.log(source)
		api.post(recharge.BalanceAdd, source).then(res => {
			// console.log(res)
			if (res.payMessage.result_code == 'SUCCESS') {
				wx.requestPayment({
					'timeStamp': res.payMessage.time, //时间戳
					'nonceStr': res.payMessage.nonce_str, //随机串
					'package': res.payMessage.prepay_id, //数据包
					'signType': res.payMessage.signType, //签名方式
					'paySign': res.payMessage.sign,
					// 'notify_url':'http://26j7g84917.qicp.vip/boda/order/notify',
					// 'notify_url':'http://backstage.dfbs-vm.com:443/boda/order/notify',
					'notify_url':'http://backstage.dfbs-vm.com:8112/boda/order/notify',
					success(res) {
						// console.log(res, 111)
						if (res.errMsg == 'requestPayment:ok') {
							// wx.showToast({
							// 	title: '充值成功',
							// 	icon: 'none'
							// })
							// wx.redirectTo({
							// 	url: '../cashWithdrawal-list/cashWithdrawal-list?type=1'
							// })
							wx.showToast({
							    title: '充值成功',
							    icon: 'none',
							    success: function () {
							      setTimeout(function() {
							       wx.redirectTo({
							       	url: '../cashWithdrawal-list/cashWithdrawal-list?type=1'
							       })
							      }, 1000);
							    }
							});
							// wx.redirectTo({
							// 	url: '../recharge/recharge'
							// })
						}
					},
					fail(res) {
						// console.log(res, 222)
					}
				})
			}
		}).catch(err => {
			wx.showToast({
				title: err.message,
				icon: 'none'
			})
		})
	}
})
